package com.hdculture.simple.expensemanager.premium;

import java.util.Date;
import java.text.DecimalFormat;

import com.hdculture.simple.expensemanager.premium.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.widget.*;
import android.util.Log;
import android.view.*;
import android.os.Bundle;

public class MainActivity extends Activity {
    
	/** Called when the activity is first created. */
	private CoreDBAdapter dbHelper;
	TextView monthText, monthIncomeText, monthExpenseText, incomeExpenseRatioText;
	ListView choiceList;
		String[] choices = {"Manage Income", "Manage Expense", "Preferences"};
	double monthIncome=0.00, monthExpense=0.00;
	String currencySign="$";
	DecimalFormat doubleDigits = new DecimalFormat("##,###.00");
	Button addIncomeButton, logoutButton, addExpenseButton;
	String tag = "MainActivity";
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
    	dbHelper.close();
    	Log.i(tag,"DB Connection Closed");
		super.onPause();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		dbHelper.open();
		Log.i(tag,"DB Connection Resumed");
		super.onResume();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbHelper = new CoreDBAdapter(this);
        dbHelper.open();
        Log.i(tag,"DB Connection Open");
        currencySign = dbHelper.getCurrencySign();
        monthText = (TextView)findViewById(R.id.monthText);
        	setMonthText();
        monthIncomeText = (TextView)findViewById(R.id.incomeText);
        monthExpenseText = (TextView)findViewById(R.id.expenseText);
        incomeExpenseRatioText = (TextView)findViewById(R.id.IERatio);
        	setMonthRatioIncomeExpense();
        choiceList = (ListView)findViewById(R.id.choiceList);
        initializeChoiceList();
        addIncomeButton = (Button)findViewById(R.id.addIncomeButton);
        	manageAddIncomeButtonClickEvent();
        addExpenseButton = (Button)findViewById(R.id.addExpenseButton);
        	manageAddExpenseButtonClickEvent();
        logoutButton = (Button)findViewById(R.id.logoutButton);
        	manageLogoutButtonClickEvent();
        updateTotalIncomeThisMonth();
        //dbHelper.createSettings("hello", "$");
    }
    public void updateTotalIncomeThisMonth(){
    	double inc=0.00, exp=0.00;
    	Cursor c = dbHelper.fetchTotalIncomeThisMonth();
    	startManagingCursor(c);
    	c.moveToFirst();
    	if(c.isAfterLast()==false){
    		String incomeString="";
    		DecimalFormat currencyFormat = new DecimalFormat(currencySign+"##,###.00");
    		inc=c.getDouble(0);
    		incomeString = currencyFormat.format(inc);
    		monthIncomeText.setText(incomeString);
    	}
    	
    	c = dbHelper.fetchTotalExpenseThisMonth();
    	startManagingCursor(c);
    	c.moveToFirst();
    	if(c.isAfterLast()==false){
    		String incomeString="";
    		DecimalFormat currencyFormat = new DecimalFormat(currencySign+"##,###.00");
    		exp = c.getDouble(0);
    		incomeString = currencyFormat.format(exp);
    		monthExpenseText.setText(incomeString);
    	}
    	double ratio=0.00;
    	DecimalFormat doubleDigits = new DecimalFormat("#0.00");
    	try{
    		ratio = (inc/exp);
    	}catch(Exception ex){
    		ratio=0.00;
    	}
    	incomeExpenseRatioText.setText("  "+doubleDigits.format(ratio));
    	if(ratio>1){
    		incomeExpenseRatioText.setTextColor(Color.GREEN);
    	}else if(ratio<0){
    		incomeExpenseRatioText.setTextColor(Color.RED);
    	}else{
    		incomeExpenseRatioText.setTextColor(Color.YELLOW);
    	}
    }
    public void setMonthText(){
    	int month = (new Date()).getMonth()+1;
        String monthString = "";
        switch(month){
        case 1:	monthString = "january";break;
        case 2: monthString = "february";break;
        case 3: monthString = "march";break;
        case 4: monthString = "april";break;
        case 5: monthString = "may";break;
        case 6: monthString = "june";break;
        case 7: monthString = "july";break;
        case 8: monthString = "august";break;
        case 9: monthString = "september";break;
        case 10: monthString = "october";break;
        case 11: monthString = "november";break;
        case 12: monthString = "december";break;
        }
        monthText.setText(monthString);
    }
    public void setMonthRatioIncomeExpense(){
    	monthIncome = getMonthIncome();
    	monthExpense = getMonthExpense();
    	monthIncomeText.setText(currencySign + monthIncome);
    	monthExpenseText.setText(currencySign + monthExpense);
    	double incomeExpenseRatio = monthIncome / monthExpense;
    	incomeExpenseRatioText.setText(doubleDigits.format(incomeExpenseRatio));
    }
    private double getMonthIncome(){
    	//Since no database has been set up, I will only assign hard-coded value
    	return 200.00;
    }
    private double getMonthExpense(){
    	//Since no database has been set up, I will only assign hard-coded value
    	return 123.00;
    }
    private void initializeChoiceList(){
    	choiceList.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,choices));
    	choiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    	
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch(arg2){
				case 0:	//Manage Income
						//Toast.makeText(MainActivity.this,choices[arg2],Toast.LENGTH_SHORT).show();
						Intent incomeIntent = new Intent(MainActivity.this,ManageIncomeActivity.class);
						startActivity(incomeIntent);
						break;
				case 1:	//Manage Expense
						//Toast.makeText(MainActivity.this,choices[arg2],Toast.LENGTH_SHORT).show();
						Intent expenseIntent = new Intent(MainActivity.this,ManageExpenseActivity.class);
						startActivity(expenseIntent);
						break;
				case 2: //Preferences
						Intent preferencesIntent = new Intent(MainActivity.this,PreferencesActivity.class);
						startActivity(preferencesIntent);
						break;
				}
			}
		});
    }
    private void manageAddIncomeButtonClickEvent(){
    	addIncomeButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				final Dialog incomeInputDialog = new Dialog(MainActivity.this);
				incomeInputDialog.setTitle("Add Income");
				
				incomeInputDialog.setContentView(R.layout.addincomedialog);
				final Button closeButton = (Button)incomeInputDialog.findViewById(R.id.cancelButton);
				final Button addIncomeButton = (Button)incomeInputDialog.findViewById(R.id.addButton);
				final EditText itemDescriptionText = (EditText)incomeInputDialog.findViewById(R.id.incomeSourceText);
				final EditText itemPriceText = (EditText)incomeInputDialog.findViewById(R.id.incomeAmountText);
				closeButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						incomeInputDialog.dismiss();
					}
				});
				addIncomeButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						//Getting the values and saving it to the database...
						dbHelper.addIncome(itemDescriptionText.getText().toString(), Double.parseDouble(itemPriceText.getText().toString()));
						Toast.makeText(MainActivity.this,"Values saved!",500).show();
						itemDescriptionText.setText("");
						itemPriceText.setText("");
					}
				});
				incomeInputDialog.show();
			}
		});
    	
    }
    private void manageAddExpenseButtonClickEvent(){
    	addExpenseButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				final Dialog expenseInputDialog = new Dialog(MainActivity.this);
				expenseInputDialog.setTitle("Add Expense");
				expenseInputDialog.setContentView(R.layout.addexpensedialog);
				final Button closeButton = (Button)expenseInputDialog.findViewById(R.id.cancelButton);
				final Button addExpenseButton = (Button)expenseInputDialog.findViewById(R.id.addButton);
				final EditText itemDescriptionText = (EditText)expenseInputDialog.findViewById(R.id.itemDescriptionText);
				final EditText itemPriceText = (EditText)expenseInputDialog.findViewById(R.id.itemPriceText);
				closeButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						expenseInputDialog.dismiss();
					}
				});
				addExpenseButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						//Getting the values and saving it to the database...
						dbHelper.addExpense(itemDescriptionText.getText().toString(), Double.parseDouble(itemPriceText.getText().toString()));
						Toast.makeText(MainActivity.this,"Values saved!",500).show();
						itemDescriptionText.setText("");
						itemPriceText.setText("");
					}
				});
				expenseInputDialog.show();
			}
		});
    }
    private void manageLogoutButtonClickEvent(){
    	logoutButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
    }
}

